<?php
/**
 * Builds up a programmatic model for stacked SQL columns clause to be used by SELECT and INSERT queries.
 * 
 * Note: MySQL uses this clause also for: REPLACE
 */
class SQLColumnsClause extends AbstractSQLClause {
	/**
	 * Sets up an columns clause (ex: select columns) directly from constructor. Syntax is array("column",...) or array("column"=>"alias",...).
	 * 
	 * @example array("col1",array("col2"=>"dd")))
	 * @param array(SQLAliasClause|string) $tblColumns
	 */
	public function __construct($tblColumns) {
		foreach($tblColumns as $mixValue) {
			if(is_array($mixValue)) {
				$this->tblContents[]=new SQLAliasClause(key($mixValue), current($mixValue));
			} else {
				$this->tblContents[]=$mixValue;
			}
		}
	}
	
	/**
	 * Transforms clause to string.
	 * 
	 * @example col1, col2 AS dd
	 * @see AbstractSQLClause::toString()
	 */
	public function toString() {
		$strOutput = "";
		if(!sizeof($this->tblContents)) return $strOutput;
		
		foreach($this->tblContents as $mixValue) {
			$strOutput .= ($mixValue instanceof SQLAliasClause?$mixValue->toString():$mixValue).", ";
		}
		
		return substr($strOutput,0,-2);
	} 
}